Chapter 07: Dimensionality Reduction II — Principal Component Analysis
1. Introduction
Feature selection discards columns. Feature extraction does something more interesting: it builds a small set of new features as linear combinations of the originals, chosen so that as much of the data's variance as possible survives the reduction. Principal Component Analysis is the canonical method.
We build PCA from its mathematical foundations — the covariance matrix, eigenvalues and eigenvectors, eigendecomposition — then state the algorithm as a learning phase (fit on training data only) and an application phase. The explained variance ratio and the scree plot give us a principled way to choose how many components to keep. We finish with a full hand-computed example on the Iris dataset and a comparative case study on Adult Census Income that pits filter selection, wrapper selection and PCA against each other on the same problem.
Learning Objectives
Explain why the direction of maximum variance is the natural first axis for a projection
Construct a covariance matrix and interpret its eigenvalues and eigenvectors
State the PCA algorithm as separate learning and application phases, and explain why the split matters
Compute the explained variance ratio and select k from a scree plot or elbow
Carry out a complete PCA by hand on a small dataset and verify that total variance is preserved
Compare filter, wrapper and PCA pipelines experimentally and interpret the train–test gap
2. Theory
2.1 Feature Extraction: Motivation of PCA
The curse of dimensionality presents several challenges:
Many ML algorithms struggle with high-dimensional data
Example: 100×100 grayscale image = 10,000 features to process
Correlated features create redundancy (e.g., height & weight both measure "size")
PCA can reduce these to fewer principal components that explain most of the variance
High dimensions make visualization impossible. PCA helps reduce to 2D or 3D for easier visualization
2.2 Principal Component Analysis (PCA)
PCA is a dimensionality reduction technique that transforms data into a new coordinate system:
PCA creates new variables (principal components) that are linear combinations of all original variables
Principal components are ordered by how much variance they explain in the data
Each principal component is orthogonal (uncorrelated) to all others
Effectively removes redundancy from correlated original features
Applications:
Face recognition
Image compression
Data visualization
Reducing dimensionality while preserving information
2.3 Finding Direction of Maximum Variance
PCA aims to find the directions of maximum variance in high-dimensional data and projects the data onto a new subspace with equal or fewer dimensions than the original one.
Key Insight: The orthogonal axes (principal components) of the new subspace can be interpreted as the directions of maximum variance given the constraint that the new feature axes are orthogonal to each other.
Mathematical Note: If vectors \(a\) and \(b\) are orthogonal, then \(a \cdot b = 0\)
2.4 Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors are fundamental mathematical concepts used in PCA:
Let \(A\) be a square matrix. A non-zero vector \(x\) is called an eigenvector of \(A\) if and only if there exists a number (real or complex) \(\lambda\) such that:
\[
A x = \lambda x
\]
If such a number \(\lambda\) exists, it is called an eigenvalue of \(A\). The vector \(x\) is called eigenvector associated to the eigenvalue.
Properties:
Eigenvectors can only be found for square matrices
Not every square matrix has eigenvectors
Given an n×n matrix that does have eigenvectors, there are n of them
For example, given a \(3 \times 3\) matrix, there are 3 eigenvectors
2.5 Mathematical Details: PCA Context
In PCA context, eigenvalues and eigenvectors have special properties:
Principal components are eigenvectors of the covariance matrix
Eigenvectors from symmetric matrices (like covariance matrices) are automatically orthogonal
This guarantees: PC1 · PC2 = 0, PC1 · PC3 = 0, PC2 · PC3 = 0, etc.
Practical meaning:
Orthogonal = perpendicular = uncorrelated
Zero dot product means no linear relationship between components
This removes redundancy that existed in original correlated features
2.6 PCA Algorithm: Step-by-Step Process
PCA consists of two phases: a Learning Phase and an Application Phase. The distinction matters in practice, because the parameters PCA learns — the feature means, the standard deviations and the projection matrix \(W\) — must be computed on the training set only. They are then applied unchanged to the validation and test data.
Learning Phase:
Standardize the d-dimensional dataset (mean=0, variance=1 for each feature)
Construct the covariance matrix
Decompose the covariance matrix into its eigenvectors and eigenvalues
Sort the eigenvalues by decreasing order to rank the corresponding eigenvectors
Application Phase:
Select k eigenvectors, which correspond to the k largest eigenvalues, where k is the dimensionality of the new feature subspace (k ≤ d)
Construct a projection matrix \(W\), from the "top" k eigenvectors
Transform the d-dimensional input dataset \(X\), using the projection matrix \(W\), to obtain the new k-dimensional feature subspace
2.7 The Transformation Matrix
We construct a \(d \times k\) transformation matrix \(W\) that maps original features to reduced dimensions:
Original data point: \(x = [x_1, x_2, ..., x_d] \in \mathbb{R}^d\)
Transformation matrix: \(W \in \mathbb{R}^{d \times k}\) (contains k selected eigenvectors as columns)
Example: 13-dimensional wine data → 3-dimensional PCA space
W is \(13 \times 3\) matrix (top 3 eigenvectors)
Each sample: \([x_1, x_2, \ldots, x_{13}] \rightarrow [\text{PC1\_score}, \text{PC2\_score}, \text{PC3\_score}]\)
Key insight: W contains the selected principal components as its columns. This is the point at which the eigenvalues and eigenvectors stop being an abstract property of the covariance matrix and become a concrete dimensionality reduction step.
2.8 Key Properties of PCA
Before working through the mathematics in detail, it is worth stating the properties that make the transformed features useful:
After transforming original d-dimensional data to k-dimensional subspace (where \(k < d\)), the first principal component captures the maximum possible variance in the data
Each subsequent principal component captures the maximum remaining variance while being orthogonal (uncorrelated) to all previous components
Even if original features are highly correlated, the resulting principal components are guaranteed to be mutually orthogonal and uncorrelated
Critical requirement: Features must be standardized before PCA if they have different scales (e.g., age in years vs. income in dollars), otherwise PCA will be dominated by features with larger numerical ranges
2.9 Eigendecomposition: Decomposing a Matrix
Eigendecomposition is at the mathematical core of PCA.
The covariance matrix is a special type of square matrix: it's symmetric, meaning the matrix equals its transpose (\(A = A^T\)).
When we decompose a symmetric matrix, we get valuable properties:
Eigenvalues are always real numbers (not complex)
Eigenvectors are orthogonal (perpendicular) to each other
This guarantees PCA components will be uncorrelated
Key insight: Eigenvalues and eigenvectors come in pairs: each eigenvalue has a corresponding eigenvector that shows the direction of variance.
Most important: The eigenvector with the largest eigenvalue points in the direction of maximum variance in the dataset—this becomes our first principal component.
2.10 Constructing the Covariance Matrix
The covariance matrix is a symmetric \(d \times d\) matrix where \(d\) is the number of features in the dataset. It stores pairwise covariances between all features, showing how each pair of features varies together.
Covariance between features \(x_j\) and \(x_k\):
\[
\sigma_{jk} = \frac{1}{n - 1}\sum_{i = 1}^{n}(x_j^{(i)} - \mu_j)(x_k^{(i)} - \mu_k)
\]
Where \(\mu_j\) and \(\mu_k\) are the sample means of features j and k respectively. Note that the sample means are zero if we standardized the dataset.
Positive covariance: Features increase/decrease together
Negative covariance: Features vary in opposite directions
Zero covariance: Features are uncorrelated
For a 3-feature dataset, the covariance matrix \(\Sigma\) looks like:
Important: The diagonal contains variances (\(\sigma_1^2, \sigma_2^2, \sigma_3^2\)), while off-diagonal elements are covariances (\(\sigma_{12}, \sigma_{13}, \sigma_{23}\)).
2.11 Explained Variance and Elbow Curve
Each principal component explains a portion of the total variance in the dataset. The explained variance helps determine how many principal components to keep.
Total Variance: The sum of variances of all original features equals the sum of variances of all principal components.
Explained Variance Ratio: The proportion of variance explained by each principal component.
s*
2.12 Explained Variance Ratio & Scree Plot
The previous section introduced explained variance informally. We now write it out in terms of the eigenvalues, which is the form used in practice to choose \(k\).
Each eigenvalue \(\lambda_j\) quantifies the variance captured by its corresponding principal component.
The proportion of total variance explained by component \(j\) is:
Let us compute PCA on the first 8 Iris samples. Because all 4 features are measured in cm with similar ranges, we only center (we do not divide by std for pedagogical clarity).
PCA preserves total variance — it just re-rotates the axes! ✓
📌 Standardization Rule-of-Thumb
We skipped full standardization here (cm features with similar scales). Always use full standardization when:
Features have different units (age in years vs. income in USD)
Features have vastly different numerical ranges (0–1 percentages vs. 10,000–1,000,000 salaries)
You want equal a-priori importance for every feature
2.14 Case Study — Feature Engineering Showdown (Adult Census Income)
We compare four feature-engineering pipelines on the Adult Income dataset
(predict income > $50K / year, ~30K rows, 14 mixed features). Evaluation metric: AUC.
Pipeline
# Features Used
Training AUC
Test AUC
Training Time
Baseline (all features)
14
0.921
0.884
1.0× (ref)
Filter (χ² + ANOVA)
9
0.910
0.881
0.8×
Wrapper (Fwd/Bwd + KNN)
7
0.932
0.876
12.4×
PCA (k = 6 components)
6
0.914
0.888
1.3×
🔍 Case Study Observations (click to expand)
Wrapper had the highest train AUC but lowest test AUC — classic overfitting to the validation folds.
PCA achieved the highest test AUC using only 6 orthogonal components — decorrelation helps generalization.
Filter was fastest, nearly matched baseline, and is a strong first choice for quick iteration.
Wrapper was ~12× slower (re-trains model for every candidate feature set) — expensive at scale.
(c) A rule-of-thumb says "keep components until ≥ 95 % variance". Roughly how many components would that be for Wine?
First 9 eigenvalues sum to ≈ 12.4, giving ≈ 95.4 %. So k ≈ 9 components.
Example 3: Pipeline Winner Interpretation
Back to the Adult Census case study. Five students interpret the results. Who is correct?
Student
Claim
Ali
Wrapper is best because its training AUC is highest (0.932).
Bilal
PCA is best because its test AUC is highest (0.888).
Chloe
Filter is best because it's fastest.
Dua
Wrapper is overfitting because test AUC (0.876) < train AUC (0.932).
Bilal and Dua are both correct.
Bilal: Test-set performance on unseen data is the gold standard for generalization.
Dua: The large train–test gap for Wrapper is textbook overfitting to validation-fold feedback.
Ali is wrong (train AUC is optimistic), and Chloe confuses speed with quality.
4. Numerical Solutions
Problem 1: PCA Calculation Example
Given a dataset with the following covariance matrix:
PC1: \((\frac{1}{\sqrt{2}}, \frac{1}{\sqrt{2}})\) with eigenvalue 3 (explains more variance)
PC2: \((\frac{1}{\sqrt{2}}, -\frac{1}{\sqrt{2}})\) with eigenvalue 1
Variance explanation: PC1 (λ=4) explains more variance than PC2 (λ=2)
Proportions:
PC1: 4/(4+2) = 4/6 = 66.7%
PC2: 2/6 = 33.3%
Problem 2: Standardization Importance
You have a dataset with two features:
Age: values range from 18 to 80 (mean=45, std=15)
Income: values range from $20,000 to $200,000 (mean=$80,000, std=$40,000)
Task: What happens if you apply PCA without standardizing the data first? Which feature will dominate the first principal component?
Solution:
Problem: Without standardization, PCA will be dominated by the feature with the largest scale (Income).
Why: PCA maximizes variance. Income has much larger absolute values and thus larger variance in the original scale, even though the relative variability (coefficient of variation) might be similar.
Result: The first principal component will be heavily weighted toward Income, and Age will have minimal influence.
Solution: Always standardize features (mean=0, variance=1) before applying PCA when features have different scales.
Problem 3: Explained Variance
After applying PCA to a dataset with 10 features, you get the following eigenvalues for the principal components: